﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using HyMaterialStatistic;

namespace YJKArch.StatisticTable
{
	internal class EscalatorGrid
	{
		public EscalatorGrid(Autodesk.Revit.DB.Document doc, GridSetCommonInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_grid = grid;
			this.m_doc = doc;
			this.m_gridSet = gridSetInfo;
		}

		public bool GetStatisticGrid(List<ViewPlan> lstViewPlan)
		{
			this.m_lstViewPlan = lstViewPlan;
			if (!this.StatisticData())
			{
				return false;
			}
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		public bool GetStatisticGrid(List<Element> lstSelElems)
		{
			this.m_lstSelElems = lstSelElems;
			if (!this.StatisticDataInSelArea())
			{
				return false;
			}
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		private bool StatisticData()
		{
			if (this.m_doc == null)
			{
				return false;
			}
			try
			{
				this.GetEscalatorInDoc();
				if (this.m_gridSet.LinkFile)
				{
					this.GetEscalatorInLinkDoc();
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
				return false;
			}
			return true;
		}

		private void GetEscalatorInDoc()
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计主文档中自动扶梯的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (Autodesk.Revit.DB.ViewPlan viewPlan in this.m_lstViewPlan)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
				Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
				transaction.Start();
				viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
				transaction.Commit();
				foreach (Element element in new FilteredElementCollector(this.m_doc, viewPlan.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_SpecialityEquipment).ToElements())
				{
					if (!this.m_dicData.ContainsKey(element.Id))
					{
						FamilyInstance familyInstance = element as FamilyInstance;
						string name = familyInstance.Symbol.Family.Name;
						if (name == "自动扶梯" || name == "自动人行道(0)" || name == "自动人行道(1-4)" || name == "自动人行道(10-12)")
						{
							List<string> list = new List<string>();
							list.Insert(0, "");
							try
							{
								string item = familyInstance.GetParameter("名称").AsString();
								list.Insert(1, item);
								if (name.Contains("人行道"))
								{
									item = familyInstance.GetParameter("人行道编号").AsString();
								}
								else
								{
									item = familyInstance.GetParameter("扶梯编号").AsString();
								}
								list.Insert(2, item);
								if (name == "自动人行道(0)")
								{
									item = "0°";
								}
								else
								{
									item = familyInstance.GetParameter("倾斜角度").AsValueString();
								}
								list.Insert(3, item);
								if (name.Contains("人行道"))
								{
									item = familyInstance.GetParameter("人行道宽度").AsValueString();
								}
								else
								{
									item = familyInstance.GetParameter("梯级宽度").AsValueString();
								}
								list.Insert(4, item);
								item = familyInstance.GetParameter("额定速度").AsString();
								list.Insert(5, item);
								item = familyInstance.GetParameter("输送能力").AsString();
								list.Insert(6, item);
								familyInstance.GetParameter("护臂板特征");
								item = familyInstance.GetParameter("护壁板特征").AsString();
								list.Insert(7, item);
								list.Insert(8, "");
							}
							catch
							{
								continue;
							}
							this.m_dicData.Add(element.Id, list);
						}
					}
				}
				transaction.Start();
				viewPlan.GetUnderLayParameter().Set(elementId);
				transaction.Commit();
			}
		}

		private void GetEscalatorInLinkDoc()
		{
			List<RevitLinkInstance> allRevitLinkInstancesInDoc = this.GetAllRevitLinkInstancesInDoc();
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count * allRevitLinkInstancesInDoc.Count, "统计链接文档中自动扶梯的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (RevitLinkInstance revitLinkInstance in allRevitLinkInstancesInDoc)
			{
				Document linkDocument = revitLinkInstance.GetLinkDocument();
				foreach (Autodesk.Revit.DB.ViewPlan viewPlan in this.m_lstViewPlan)
				{
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						return;
					}
					ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
					Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
					transaction.Commit();
					foreach (Element element in new FilteredElementCollector(linkDocument, viewPlan.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_SpecialityEquipment).ToElements())
					{
						if (!this.m_dicData.ContainsKey(element.Id))
						{
							FamilyInstance familyInstance = element as FamilyInstance;
							string name = familyInstance.Symbol.Family.Name;
							if (name == "自动扶梯" || name == "自动人行道(0)" || name == "自动人行道(1-4)" || name == "自动人行道(10-12)")
							{
								List<string> list = new List<string>();
								list.Insert(0, "");
								try
								{
									string item = familyInstance.GetParameter("名称").AsString();
									list.Insert(1, item);
									if (name.Contains("人行道"))
									{
										item = familyInstance.GetParameter("人行道编号").AsString();
									}
									else
									{
										item = familyInstance.GetParameter("扶梯编号").AsString();
									}
									list.Insert(2, item);
									item = familyInstance.GetParameter("倾斜角度").AsValueString();
									list.Insert(3, item);
									if (name.Contains("人行道"))
									{
										item = familyInstance.GetParameter("人行道宽度").AsValueString();
									}
									else
									{
										item = familyInstance.GetParameter("梯级宽度").AsValueString();
									}
									list.Insert(4, item);
									item = familyInstance.GetParameter("额定速度").AsString();
									list.Insert(5, item);
									item = familyInstance.GetParameter("输送能力").AsString();
									list.Insert(6, item);
									item = familyInstance.GetParameter("护壁板特征").AsString();
									list.Insert(7, item);
									list.Insert(8, "");
								}
								catch
								{
									continue;
								}
								this.m_dicData.Add(element.Id, list);
							}
						}
					}
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(elementId);
					transaction.Commit();
				}
			}
		}

		private bool StatisticDataInSelArea()
		{
			try
			{
				ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstSelElems.Count, "统计区域中自动扶梯的信息", ProgressBarStyle.Blocks, true, false);
				progressBarForm.Show(WindowHelper.HWndRevit);
				foreach (Element element in this.m_lstSelElems)
				{
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						return false;
					}
					if (!this.m_dicData.ContainsKey(element.Id))
					{
						FamilyInstance familyInstance = element as FamilyInstance;
						string name = familyInstance.Symbol.Family.Name;
						if (name == "自动扶梯" || name == "自动人行道(0)" || name == "自动人行道(1-4)" || name == "自动人行道(10-12)")
						{
							List<string> list = new List<string>();
							list.Insert(0, "");
							try
							{
								string item = familyInstance.GetParameter("名称").AsString();
								list.Insert(1, item);
								if (name.Contains("人行道"))
								{
									item = familyInstance.GetParameter("人行道编号").AsString();
								}
								else
								{
									item = familyInstance.GetParameter("扶梯编号").AsString();
								}
								list.Insert(2, item);
								item = familyInstance.GetParameter("倾斜角度").AsValueString();
								list.Insert(3, item);
								if (name.Contains("人行道"))
								{
									item = familyInstance.GetParameter("人行道宽度").AsValueString();
								}
								else
								{
									item = familyInstance.GetParameter("梯级宽度").AsValueString();
								}
								list.Insert(4, item);
								item = familyInstance.GetParameter("额定速度").AsString();
								list.Insert(5, item);
								item = familyInstance.GetParameter("输送能力").AsString();
								list.Insert(6, item);
								item = familyInstance.GetParameter("护壁板特征").AsString();
								list.Insert(7, item);
								list.Insert(8, "");
							}
							catch
							{
								continue;
							}
							this.m_dicData.Add(element.Id, list);
						}
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
				return false;
			}
			return true;
		}

		private List<RevitLinkInstance> GetAllRevitLinkInstancesInDoc()
		{
			IEnumerable<ElementId> enumerable = new FilteredElementCollector(this.m_doc).OfClass(typeof(RevitLinkInstance)).ToElementIds();
			List<RevitLinkInstance> list = new List<RevitLinkInstance>();
			foreach (ElementId elementId in enumerable)
			{
				Element element = this.m_doc.GetElement(elementId);
				if (!(element.GetType() != typeof(RevitLinkInstance)))
				{
					RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
					if (revitLinkInstance != null)
					{
						list.Add(revitLinkInstance);
					}
				}
			}
			return list;
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_gridSet.GridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSet.TitleFontSize;
			int num = 2;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			Dictionary<ElementId, List<string>>.Enumerator enumerator = this.m_dicData.GetEnumerator();
			int count = this.m_dicData.Count;
			if (count == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + count + 1;
			foreach (KeyValuePair<ElementId, List<string>> keyValuePair in this.m_dicData)
			{
				List<string> value = keyValuePair.Value;
				num++;
				for (int i = 1; i < value.Count; i++)
				{
					this.m_grid.Cell(num, i).Text = value[i];
					this.m_grid.Cell(num, i).Alignment = AlignmentEnum.CenterCenter;
				}
				this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).Sort(2, SortTypeEnum.Text, SortEnum.Ascending);
			}
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSet.FontSize;
			if (this.m_gridSet.SetColWidth)
			{
				this.m_grid.Range(3, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			}
			else
			{
				this.m_grid.Range(3, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = false;
			}
			for (int j = 1; j < this.m_grid.Rows; j++)
			{
				this.m_grid.Row(j).AutoFit();
			}
			if (!this.m_gridSet.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private FlexCell.Grid m_grid;

		private Autodesk.Revit.DB.Document m_doc;

		private GridSetCommonInfo m_gridSet;

		private Dictionary<ElementId, List<string>> m_dicData = new Dictionary<ElementId, List<string>>();

		private List<ViewPlan> m_lstViewPlan;

		private List<Element> m_lstSelElems;
	}
}
